package com.tarena.lbs.basic.web.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.dao.repository.BussinessCategoryRepository;
import com.tarena.lbs.basic.web.dao.repository.BussinessRepository;
import com.tarena.lbs.pojo.basic.po.BusinessCategoryPO;
import com.tarena.lbs.pojo.basic.po.BusinessPO;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import com.tarena.lbs.pojo.basic.vo.BusinessCategoryVO;
import com.tarena.lbs.pojo.basic.vo.BusinessVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BussinessService {

    @Autowired
    private BussinessRepository bussinessRepository;
    @Autowired
    private BussinessCategoryRepository bussinessCategoryRepository;

    public PageResult<BusinessVO> getPageBusinessVO(BusinessQuery query) {
        PageResult<BusinessVO> voPageResult = new PageResult<>();
        voPageResult.setPageNo(query.getPageNo());
        voPageResult.setPageSize(query.getPageSize());


        Page<BusinessPO> page = getPage(query);
        List<BusinessPO> listBusinessPO = page.getRecords();

        List<BusinessVO> listVO = listBusinessPO.stream().map((obj) -> {
            BusinessVO businessVO = new BusinessVO();
            BeanUtils.copyProperties(obj, businessVO);
            return businessVO;
        }).collect(Collectors.toList());

        voPageResult.setObjects(listVO);
        voPageResult.setTotal(page.getTotal());

        return voPageResult;
    }

    private Page<BusinessPO> getPage(BusinessQuery query) {
        Page<BusinessPO> page = new Page<>(query.getPageNo(),query.getPageSize());

        Page<BusinessPO> result = bussinessRepository.page(page);
        //转化为list

        return result;
    }

    public List<BusinessCategoryVO> getBusinessCategoryVO() {
        List<BusinessCategoryPO> list = bussinessCategoryRepository.list();

        List<BusinessCategoryVO> collect = list.stream().map((obj) -> {
            BusinessCategoryVO businessCategoryVO = new BusinessCategoryVO();
            BeanUtils.copyProperties(obj, businessCategoryVO);
            return businessCategoryVO;
        }).collect(Collectors.toList());

        return collect;
    }
}


